package com.ngc.mall.controller;

import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.ThemeDomain;
import com.ngc.mall.param.ThemeParam;
import com.ngc.mall.service.IThemeService;
import com.ngc.mall.vo.ThemeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 *  专题
 *
 * @author : Chen Xiao Lin
 * @Date : 2023/5/15 9:48
 **/

@RestController
@RequestMapping("/theme")
@Api(tags = "专题")
public class ThemeController {

    @Resource
    private IThemeService iThemeService;

    /**
     * 新建专题
     * @return
     */
    @PostMapping("")
    @ApiOperation("新增专题")
    public ResponseResult<Long> createTheme(@RequestBody ThemeParam themeParam){
        ThemeDomain themeDomain=ThemeDomain.builder()
                .name(themeParam.getName())
                .description(themeParam.getDescription())
                .entranceImg(themeParam.getEntranceImg())
                .internalTopImg(themeParam.getInternalTopImg())
                .sort(themeParam.getSort())
                .isEnabled(themeParam.getIsEnabled())
                .build();
        return ResponseResult.success(iThemeService.createTheme(themeDomain));
    }

    /**
     * 更新专题
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value="更新专题")

    public ResponseResult<Boolean> updateThemeById(@PathVariable Long id,@RequestBody ThemeParam themeParam ){
        ThemeDomain themeDomain=ThemeDomain.builder()
                .description(themeParam.getDescription())
                .entranceImg(themeParam.getEntranceImg())
                .internalTopImg(themeParam.getInternalTopImg())
                .sort(themeParam.getSort())
                .isEnabled(themeParam.getIsEnabled())
                .build();
        return ResponseResult.auto(iThemeService.updateThemeById(id,themeDomain));
    }

    /**
     * 删除专题
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除专题")
    public ResponseResult<Boolean> deleteThemeById(@PathVariable Long id){
        return ResponseResult.auto(iThemeService.removeById(id));
    }

    /**
     * 查看专题所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查看所有专题")
    public ResponseResult<List<ThemeDomain>> selectAllTheme(){
        return ResponseResult.success(iThemeService.list());
    }

    /**
     * 根据id查看专题详情（包括其下方关联的spu）
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value="根据id查看专题详情")
    public ResponseResult<ThemeVO> getDetailById(@PathVariable Long id){
        return ResponseResult.success(iThemeService.getDetailById(id));
    }

    /**
     * 更新专题上/下架状态
     */
    @PutMapping("/status/{id}")
    @ApiOperation(value = "更新专题上/下架状态")
    public ResponseResult<Boolean> updateStatusById(@PathVariable Long id,@RequestParam boolean isEnabled){
        return ResponseResult.auto(iThemeService.updateStatusById(id,isEnabled));
    }

}
